package com.example.rlw.mvpplugintest.adapter;

import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.ViewGroup;

import com.example.rlw.mvpplugintest.mvp.MVPBaseFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * ViewPageAdapter
 */
public class MyPagerAdapter extends FragmentPagerAdapter {
    private List<MVPBaseFragment> mFragments = new ArrayList<>();
    private List<String> title;

    public MyPagerAdapter(FragmentManager fm, List<MVPBaseFragment> fragments) {
        super(fm);
        if (fragments != null) {
            mFragments.addAll(fragments);
        }
    }

    public MyPagerAdapter(FragmentManager fm, List<MVPBaseFragment> fragments, List<String> title) {
        super(fm);
        if (fragments != null) {
            mFragments.addAll(fragments);
        }
        this.title = title;
    }

    @Override
    public Fragment getItem(int position) {
        MVPBaseFragment fragment = null;
        if (position >= 0 && position < getCount()) {
            fragment = mFragments.get(position);
        }
        return fragment;
    }

    @Override
    public int getCount() {
        return mFragments.size();
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {

    }

    /**
     * 刷新界面的Fragment
     *
     * @param fragments :
     */
    public void update(List<MVPBaseFragment> fragments) {
        mFragments.clear();
        if (fragments != null) {
            mFragments.addAll(fragments);
        }
        notifyDataSetChanged();
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return title.get(position);
    }
}
